﻿using FarmerJoe.Helpers;
using FarmerJoe.Settings;
using Styx;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;

namespace FarmerJoe.Classes
{
    public static partial class Druid
    {
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private static WoWUnit CT { get { return Me.CurrentTarget; } }

        #region Specialisation Switchs
        // Determine which routine to use based on your specialisation
        //
        public static Composite CombatBehavior_SpecSwitch()
        {
            return new PrioritySelector(

                new Switch<WoWSpec>(ret => Me.Specialization,
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidBalance, CombatBehavior_Balance),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.None, CombatBehavior_LowLevel),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidFeral, CombatBehavior_Feral),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidRestoration, Behaviour_Restoration(ClassParser.CCControlledMovement)),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidGuardian, CombatBehavior_Guardian)
                ));
        }

        public static Composite PullBehavior_SpecSwitch()
        {
            return new PrioritySelector(

                new Switch<WoWSpec>(ret => Me.Specialization,
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidBalance, PullBehavior_Balance),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.None, CombatBehavior_LowLevel),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidFeral, PullBehavior_Feral),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidRestoration, Behaviour_Restoration(ClassParser.CCControlledMovement)),
                                                 new SwitchArgument<WoWSpec>(WoWSpec.DruidGuardian, PullBehavior_Guardian)
                ));
        }
        #endregion

        // Do we have enough mana to cast the spell and get back to Cat Form
        public static bool EnoughMana(string spellName)
        {
            if (!SpellManager.HasSpell(spellName)) return false;

            string shiftedForm = "Cat Form";
            int formMana = 3000;
            int spellMana = SpellManager.Spells[spellName].PowerCost;

            switch (Me.Shapeshift)
            {
                    case ShapeshiftForm.Cat:
                    shiftedForm = "Cat Form";
                    break;

                    case ShapeshiftForm.Bear:
                    shiftedForm = "Bear Form";
                    break;

                    case ShapeshiftForm.Moonkin:
                    shiftedForm = "Moonkin Form";
                    break;
            }

            formMana = SpellManager.Spells[shiftedForm].PowerCost;

            // Add a small bit for padding purposes
            return Me.CurrentMana >= (formMana + spellMana) *1.5;
        }

        // todo Add support for other specifications of Nature's Vigil. Currently only FERAL
        public static Composite AllFormsNaturesVigil()
        {
            return new PrioritySelector(ret => !FpswareSettings.Instance.Druid.FeralNaturesVigilWhen.Contains("never"),

                                        new Decorator( ret => (Me.Specialization == WoWSpec.None && Me.Shapeshift == ShapeshiftForm.Cat) || (Me.Specialization == WoWSpec.DruidBalance && Me.Shapeshift == ShapeshiftForm.Cat) || Me.Specialization == WoWSpec.DruidFeral,
                                            new PrioritySelector(
                                                Spells.Heal("Nature's Vigil", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralNaturesVigilHealth && CLC.ResultOK( FpswareSettings.Instance.Druid.FeralNaturesVigilWhen))
                                                )),


                                        new Decorator( ret => (Me.Specialization == WoWSpec.DruidFeral && Me.Shapeshift == ShapeshiftForm.Bear) || Me.Specialization == WoWSpec.DruidGuardian,
                                            new PrioritySelector(
                                                Spells.Heal("Nature's Vigil", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralNaturesVigilHealth && CLC.ResultOK( FpswareSettings.Instance.Druid.FeralNaturesVigilWhen))
                                                )),


                                        new Decorator( ret => Me.Specialization == WoWSpec.DruidBalance || Me.Specialization == WoWSpec.None, 
                                            new PrioritySelector(
                                                Spells.Heal("Nature's Vigil", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralNaturesVigilHealth && CLC.ResultOK( FpswareSettings.Instance.Druid.FeralNaturesVigilWhen))
                                                )),


                                        new Decorator(ret => Me.Specialization == WoWSpec.DruidRestoration,
                                            new PrioritySelector( 
                                                Spells.Heal("Nature's Vigil", ret => Me.HealthPercent < FpswareSettings.Instance.Druid. FeralNaturesVigilHealth && CLC.ResultOK( FpswareSettings.Instance.Druid. FeralNaturesVigilWhen))
                                                          ))
                );
        }

        // Healing. Based on currently shapeshifted form and/or specialisation
        public static Composite AllFormsHeal()
        {
            return new PrioritySelector(
                Spells.Heal("Innervate", ret => Me.ManaPercent <= FpswareSettings.Instance.Druid.InnervateMana),

                new Decorator(ret => (Me.Specialization == WoWSpec.None && Me.Shapeshift == ShapeshiftForm.Cat) || (Me.Specialization == WoWSpec.DruidBalance && Me.Shapeshift == ShapeshiftForm.Cat) || Me.Specialization == WoWSpec.DruidFeral,
                              new PrioritySelector(
                                  Spells.Heal("Barkskin", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralBarkskinHealth),
                                  Spells.Heal("Renewal", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralRenewal),
                                  //new Decorator(ret=> Me.AuraTimeLeft("Rejuvenation") < 3, new Decorator(ret=> Me.HealthPercent < FpswareSettings.Instance.Druid.FeralRejuvenation || )),
                                  Spells.Heal("Rejuvenation", ret => (Me.HealthPercent < FpswareSettings.Instance.Druid.FeralRejuvenation && !Me.HasAura("Rejuvenation") && EnoughMana("Rejuvenation"))),
                                  Spells.Heal("Healing Touch", ret => Me.HealthPercent < 85 && Me.HasAura("Predatory Swiftness")),
                                  Spells.Heal("Healing Touch", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralHealingTouch && Timers.SpellOkToCast("Cat Healing Touch", 6000) && EnoughMana("Healing Touch")),
                                  Spells.Heal("Cenarion Ward", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FeralCenarionWard)
                                  )),


                new Decorator(ret => (Me.Specialization == WoWSpec.DruidFeral && Me.Shapeshift == ShapeshiftForm.Bear) || Me.Specialization == WoWSpec.DruidGuardian,
                              new PrioritySelector(
                                  Spells.Heal("Barkskin", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.GuardianBarkskinHealth),
                                  Spells.Heal("Renewal", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.GuardianRenewal),
                                  Spells.Heal("Rejuvenation", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.GuardianRejuvenation && !Me.HasAura("Rejuvenation") && EnoughMana("Rejuvenation")),
                                  Spells.Heal("Healing Touch", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.GuardianHealingTouch && EnoughMana("Healing Touch")),
                                  Spells.Heal("Cenarion Ward", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.GuardianCenarionWard),
                                  Spells.Heal("Frenzied Regeneration", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.FrenziedRegeneration && Me.CurrentRage >= 60 && Timers.SpellOkToCast("Frenzied Regen", 3000)),
                                  Spells.Cast("Savage Defense", ret => (CLC.ResultOK(FpswareSettings.Instance.Druid.SavageDefense) && Me.HealthPercent < FpswareSettings.Instance.Druid.SavageDefenseHealth) && !Me.HasAura("Savage Defense")),
                                  Spells.Cast("Survival Instincts", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.GuardianSurvivalInstincts),
                                  Spells.Cast("Might of Ursoc", ret => !FpswareSettings.Instance.Druid.MightOfUrsocBear.Contains("never") && Me.HealthPercent < FpswareSettings.Instance.Druid.MightOfUrsocBearHealth && CLC.ResultOK(FpswareSettings.Instance.Druid.MightOfUrsocBearWhen))
                                  )),


                new Decorator(ret => Me.Specialization == WoWSpec.DruidBalance || Me.Specialization == WoWSpec.None,
                              new PrioritySelector(
                                  Spells.Heal("Barkskin", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.BalanceBarkskinHealth),
                                  Spells.Heal("Renewal", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.BalanceRenewal),
                                  Spells.Heal("Rejuvenation", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.BalanceRejuvenation && !Me.HasAura("Rejuvenation") && EnoughMana("Rejuvenation")),
                                  Spells.Heal("Healing Touch", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.BalanceHealingTouch && EnoughMana("Healing Touch")),
                                  Spells.Heal("Cenarion Ward", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.BalanceCenarionWard)
                                  )),


                new Decorator(ret => Me.Specialization == WoWSpec.DruidRestoration,
                              new PrioritySelector(
                                  ))

                );
        }

        // Used by all specs
        public static Composite PreCombatBuffs()
        {
            return new PrioritySelector(

                Spells.WaitForCast(),

                new Decorator(ret => !FpswareSettings.Instance.Druid.NonCombatHealing.Contains("never"),
                              new PrioritySelector(
                                  Spells.Heal("Healing Touch", ret => Me.HealthPercent < 95 && Me.HasAura("Predatory Swiftness")),
                                  Spells.Heal("Rejuvenation", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.NonCombatRejuvenationHealth && Me.AuraTimeLeft("Rejuvenation") < 3),
                                  Spells.Heal("Healing Touch", ret => Me.HealthPercent < FpswareSettings.Instance.Druid.NonCombatHealingTouchHealth)
                                  )),

                Spells.BuffSelf("Mark of the Wild", ret => !FpswareSettings.Instance.Druid.MarkOfTheWild.Contains("never") && !Me.HasAura("Mark of the Wild") && !Me.HasAura("Blessing of Kings") && !Me.HasAura("Legacy of the Emperor") && !Me.HasAura("Embrace of the Shale Spider") && !Me.HasAura("Prowl")),

                new Decorator(ret => Me.IsCasting || Me.IsChanneling, new Action(context => RunStatus.Success))

                );
        }



    }
}
